﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;

namespace DALL.Order
{
   public class T_PUB_Order_Project
    {

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
       public List<Model.Order.T_PUB_Order_Projectcs> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,OriderID,ProjectID,Number,Remark ");
            strSql.Append(" FROM T_PUB_Order_Project ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.Order.T_PUB_Order_Projectcs> list = new List<Model.Order.T_PUB_Order_Projectcs>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.Order.T_PUB_Order_Projectcs ReaderBind(IDataReader dataReader)
        {
            Model.Order.T_PUB_Order_Projectcs model = new Model.Order.T_PUB_Order_Projectcs();
            object ojb;
            ojb = dataReader["id"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.id = (int)ojb;
            }
            model.OriderID = dataReader["OriderID"].ToString();
            model.ProjectID = dataReader["ProjectID"].ToString();
            ojb = dataReader["Number"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Number = (int)ojb;
            }
            model.Remark = dataReader["Remark"].ToString();
            return model;
        }

        public DataTable GetOrderProject(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT p.PROJECT_ID, \n");
            varname1.Append("       o_p.Number, \n");
            varname1.Append("       p.PROJECT_NAME, \n");
            varname1.Append("       p.PRICE              AS projectPrice, \n");
            varname1.Append("       p_m.PROJECT_ID       mpid, \n");
            varname1.Append("       pt.PROJECTTYPE_NAME, \n");
            varname1.Append("       m.MATERIAL_NAME, \n");
            varname1.Append("       m.PRICE, \n");
            varname1.Append("       m.IMAGEURL, \n");
            varname1.Append("       p_m.ID, \n");
            varname1.Append("       p_m.NUM, \n");
            varname1.Append("       mt.TYPE_NAME, \n");
            varname1.Append("       p_m.NUM * o_p.Number AS total \n");
            varname1.Append("FROM   dbo.T_PUB_Order_Project o_p \n");
            varname1.Append("       LEFT JOIN dbo.T_PUB_PROJECT p \n");
            varname1.Append("         ON p.PROJECT_ID = o_p.ProjectID \n");
            varname1.Append("       LEFT JOIN dbo.T_PUB_PROJECTTYPE pt \n");
            varname1.Append("         ON p.PROJECTTYPE_ID = pt.PROJECTTYPE_ID \n");
            varname1.Append("       LEFT JOIN dbo.T_PUB_PROJECTMATERIAL p_m \n");
            varname1.Append("         ON p.PROJECT_ID = p_m.PROJECT_ID \n");
            varname1.Append("       LEFT JOIN dbo.T_PUB_MATERIAL m \n");
            varname1.Append("         ON p_m.MATERIAL_ID = m.MATERIAL_ID \n");
            varname1.Append("       LEFT JOIN dbo.T_PUB_MATERIALTYPE mt \n");
            varname1.Append("         ON m.TYPE_ID = mt.TYPE_ID ");
            if (strWhere!="")
            {
                varname1.Append( " where 1>0 "+strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString()).Tables[0];
        }
    }
}
